#cmake 最低版本 3.15以上才支持armcc
cmake_minimum_required(VERSION 3.27.4)
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CPU Cortex-M4)

project(FreeModbus C CXX ASM)
# 在build生成编译选项，用于生成compile_commands.json，可以给clangd用
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)


#添加全局宏定义
add_definitions(-DSTM32F40_41xxx -DUSE_STDPERIPH_DRIVER)
add_definitions(-DHDL_USING_AUTO_INIT)
# set(__ARMCC_VERSION 5060960)
# add_definitions(-D__CC_ARM)
# add_definitions(-D__TARGET_FPU_VFP)


#自定义的变量，指定C语言标准为C99
# set(CMAKE_C_FLAGS "--c99") # that would be bad   

#设置编译器flag
set(CMAKE_C_FLAGS "--cpu=${CPU} --c99 -O0")
set(CMAKE_CXX_FLAGS "--cpu=${CPU}")
set(CMAKE_ASM_FLAGS "--cpu=${CPU}.fp.sp -g --apcs=interwork")

         

#   set(CMAKE_ASM_MASM_COMPILER ${CMAKE_ASM_COMPILER})
message("-- CMAKE_ASM_COMPILER explicitly set to: ${CMAKE_ASM_COMPILER}")

# Enable generic assembly compilation to avoid CMake generate VS proj files that explicitly
# use ml[64].exe as the assembler.
enable_language(ASM)
# set(CMAKE_ASM_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreaded         "")
# set(CMAKE_ASM_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDLL      "")
# set(CMAKE_ASM_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDebug    "")
# set(CMAKE_ASM_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDebugDLL "")
# 重新定义CMAKE_ASM_COMPILE_OBJECT并创建自己的属性来获得更细粒度的控制
set(CMAKE_ASM_COMPILE_OBJECT "<CMAKE_ASM_COMPILER> -g <INCLUDES> <FLAGS> -o <OBJECT> <SOURCE>")

# 默认开启调试输出选项
option(DEBUG "Enable debug output" ON)  
if(DEBUG)
# 定义DEBUG宏，可以在代码中使用条件编译来控制输出内容
    add_definitions(-DDEBUG)  
endif()


#头文件路径添加
include_directories(
    ${PROJECT_SOURCE_DIR}/sources/APL
    ${PROJECT_SOURCE_DIR}/sources/FML/FreeRTOS/include
    ${PROJECT_SOURCE_DIR}/sources/FML/FreeRTOS/portable/RVDS/ARM_CM4F
    ${PROJECT_SOURCE_DIR}/sources/HAL/CORE
    ${PROJECT_SOURCE_DIR}/sources/HAL/FWLIB/inc
    ${PROJECT_SOURCE_DIR}/sources/HDL/delay
    ${PROJECT_SOURCE_DIR}/sources/FML/FreeModbus/modbus/include
    ${PROJECT_SOURCE_DIR}/sources/FML/FreeModbus/modbus/rtu
    ${PROJECT_SOURCE_DIR}/sources/FML/FreeModbus/port
    ${PROJECT_SOURCE_DIR}/sources/BLL/includes
    ${PROJECT_SOURCE_DIR}/sources/HDL/AUTOINIT
    ${PROJECT_SOURCE_DIR}/sources/HDL/LED
    ${PROJECT_SOURCE_DIR}/sources/DAL/LED
    ${PROJECT_SOURCE_DIR}/sources/HDL/RNG
    ${PROJECT_SOURCE_DIR}/sources/DAL/RNG
    ${PROJECT_SOURCE_DIR}/sources/HDL/LOG
    ${PROJECT_SOURCE_DIR}/sources/DAL/LOG
    ${PROJECT_SOURCE_DIR}/sources/HDL/DEM
    ${PROJECT_SOURCE_DIR}/sources/DAL/DEM
)

#链接选项
#其中最重要的就是指定sct文件路径，该文件需要先试用keil编译生成一次。
add_link_options(--branchpatch=sdcomp-29491-629360 --strict --summary_stderr
                --info sizes,totals,unused,veneers --load_addr_map_info --xref --callgraph --symbols)
add_link_options(--scatter=${CMAKE_SOURCE_DIR}/sources/PROJECT/Objects/FreeModbus.sct)

#源文件路径添加
file(GLOB_RECURSE SOURCES
    ${PROJECT_SOURCE_DIR}/sources/HAL/CORE/startup_stm32f40_41xxx.s
    ${PROJECT_SOURCE_DIR}/sources/HAL/FWLIB/src/*.c
    ${PROJECT_SOURCE_DIR}/sources/APL/*.c
    ${PROJECT_SOURCE_DIR}/sources/FML/FreeRTOS/*.c
    ${PROJECT_SOURCE_DIR}/sources/FML/FreeRTOS/portable/MemMang/heap_4.c
    ${PROJECT_SOURCE_DIR}/sources/FML/FreeRTOS/portable/RVDS/ARM_CM4F/port.c
    ${PROJECT_SOURCE_DIR}/sources/FML/FreeModbus/modbus/functions/*.c
    ${PROJECT_SOURCE_DIR}/sources/FML/FreeModbus/modbus/rtu/*.c
    ${PROJECT_SOURCE_DIR}/sources/FML/FreeModbus/modbus/*.c
    ${PROJECT_SOURCE_DIR}/sources/FML/FreeModbus/port/*.c
    ${PROJECT_SOURCE_DIR}/sources/BLL/sources/*.c
    ${PROJECT_SOURCE_DIR}/sources/HDL/AUTOINIT/*.c
    ${PROJECT_SOURCE_DIR}/sources/HDL/LED/*.c
    ${PROJECT_SOURCE_DIR}/sources/DAL/LED/*.c
    ${PROJECT_SOURCE_DIR}/sources/HDL/RNG/*.c
    ${PROJECT_SOURCE_DIR}/sources/DAL/RNG/*.c
    ${PROJECT_SOURCE_DIR}/sources/HDL/LOG/*.c
    ${PROJECT_SOURCE_DIR}/sources/DAL/LOG/*.c
     ${PROJECT_SOURCE_DIR}/sources/HDL/DEM/*.c
    ${PROJECT_SOURCE_DIR}/sources/DAL/DEM/*.c
)
# 添加主要的可执行文件或者库
add_executable(${PROJECT_NAME})

# target_compile_definitions(${PROJECT_NAME} PRIVATE __ARMCC_VERSION=${__ARMCC_VERSION})

target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})

#生成hex和bin文件
set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin)

# 打印代码和数据大小
# add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
#     COMMAND ${ARMCC_FROMELF} --text -z $<TARGET_FILE:${PROJECT_NAME} > COMMENT "Print memory info")

# 添加自定义命令来生成.hex和.bin文件
message("-- ARMCC_FROMELF explicitly set to: ${CMAKE_OBJCOPY}")
# add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
#     COMMAND ${ARMCC_FROMELF} --output ${HEX_FILE} --i32 $<TARGET_FILE:${PROJECT_NAME}>
#     COMMAND ${ARMCC_FROMELF} --output ${BIN_FILE} --bin $<TARGET_FILE:${PROJECT_NAME}>
#     COMMENT "Build ${HEX_FILE} and ${BIN_FILE}")